#include "PositionGroupBox.h"

PositionGroupBox::PositionGroupBox(QWidget *parent)
    :QGroupBox(parent),
      m_xorg(0),
      m_yorg(0),
      m_zorg(0),
      m_xmmps(0),
      m_ymmps(0),
      m_zmmps(0)

{
    initContent();
    initConnection();
    setPosition(0, 0, 0);
}

void PositionGroupBox::enableXYPosition(bool on)
{
    labelX->setVisible(on);
    labelY->setVisible(on);
    //labelZ->setVisible(on);

    labelXr->setVisible(on);
    labelYr->setVisible(on);
    //labelZr->setVisible(on);

    labelPosX->setVisible(on);
    labelPosY->setVisible(on);

    labelPosXRel->setVisible(on);
    labelPosYRel->setVisible(on);

    pushButtonZeroX->setVisible(on);
    pushButtonZeroY->setVisible(on);
}

void PositionGroupBox::setXPosition(double x)
{
    m_xmmps = x;
    labelPosX->setNum(m_xmmps);
    labelPosXRel->setNum(m_xmmps - m_xorg);
}

void PositionGroupBox::setYPosition(double y)
{
    m_ymmps = y;
    labelPosY->setNum(m_ymmps);
    labelPosYRel->setNum(m_ymmps - m_yorg);
}

void PositionGroupBox::setZPosition(double z)
{
    m_zmmps = z;
    labelPosZ->setNum(m_zmmps);
    labelPosZRel->setNum(m_zmmps - m_zorg);
}

void PositionGroupBox::setPosition(double x, double y, double z)
{
    setXPosition(x);
    setYPosition(y);
    setZPosition(z);
}

void PositionGroupBox::initContent()
{
    setTitle("Position");
    gridLayout = new QGridLayout(this);
    labelX = new QLabel;
    labelX->setText("X Position");
    gridLayout->addWidget(labelX, 0, 0, 1, 1);

    labelY = new QLabel;
    labelY->setText("Y Position");
    gridLayout->addWidget(labelY, 0, 1, 1, 1);

    labelZ = new QLabel;
    labelZ->setText("Z Position");
    gridLayout->addWidget(labelZ, 0, 2, 1, 1);

    labelPosX = new QLabel(this);
    labelPosX->setFrameShape(QFrame::Box);
    gridLayout->addWidget(labelPosX, 1, 0, 1, 1);

    labelPosY = new QLabel(this);
    labelPosY->setFrameShape(QFrame::Box);
    gridLayout->addWidget(labelPosY, 1, 1, 1, 1);

    labelPosZ = new QLabel(this);
    labelPosZ->setFrameShape(QFrame::Box);
    gridLayout->addWidget(labelPosZ, 1, 2, 1, 1);

    labelXr = new QLabel;
    labelXr->setText("X Rel Pos");
    gridLayout->addWidget(labelXr, 2, 0, 1, 1);

    labelYr = new QLabel;
    labelYr->setText("Y Rel Pos");
    gridLayout->addWidget(labelYr, 2, 1, 1, 1);

    labelZr = new QLabel;
    labelZr->setText("Z Rel Pos");
    gridLayout->addWidget(labelZr, 2, 2, 1, 1);


    labelPosXRel = new QLabel(this);
    labelPosXRel->setFrameShape(QFrame::Box);
    gridLayout->addWidget(labelPosXRel, 3, 0, 1, 1);

    labelPosYRel = new QLabel(this);
    labelPosYRel->setFrameShape(QFrame::Box);
    gridLayout->addWidget(labelPosYRel, 3, 1, 1, 1);

    labelPosZRel = new QLabel(this);
    labelPosZRel->setFrameShape(QFrame::Box);
    gridLayout->addWidget(labelPosZRel, 3, 2, 1, 1);

    pushButtonZeroX = new QPushButton(this);
    pushButtonZeroX->setText("Set Zero");
    gridLayout->addWidget(pushButtonZeroX, 4, 0, 1, 1);

    pushButtonZeroY = new QPushButton(this);
    pushButtonZeroY->setText("Set Zero");
    gridLayout->addWidget(pushButtonZeroY, 4, 1, 1, 1);

    pushButtonZeroZ = new QPushButton(this);
    pushButtonZeroZ->setText("Set Zero");
    gridLayout->addWidget(pushButtonZeroZ, 4, 2, 1, 1);

}

void PositionGroupBox::on_ButtonZeroX_click()
{
    m_xorg = m_xmmps;
    labelPosXRel->setNum(m_xmmps - m_xorg);
}

void PositionGroupBox::on_ButtonZeroY_click()
{
    m_yorg = m_ymmps;
    labelPosYRel->setNum(m_ymmps - m_yorg);
}

void PositionGroupBox::on_ButtonZeroZ_click()
{
    m_zorg = m_zmmps;
    labelPosZRel->setNum(m_zmmps - m_zorg);
}

void PositionGroupBox::initConnection()
{
    connect(pushButtonZeroX, SIGNAL(clicked(bool)), this, SLOT(on_ButtonZeroX_click()));
    connect(pushButtonZeroY, SIGNAL(clicked(bool)), this, SLOT(on_ButtonZeroY_click()));
    connect(pushButtonZeroZ, SIGNAL(clicked(bool)), this, SLOT(on_ButtonZeroZ_click()));
}
